
*********************************************************************************
** PRELIMINARIES
********************************************************************************

********************************************************************
** WAVE 3 POST-HARVEST DATA
********************************************************************

** Attitudes (household head only)
	use "$dir/rawdata/LSMS Nigeria/2015/sect6b_harvestw3.dta", clear // ATTITUDES

	** Score the CESD
	gen score=0 if s6bq01==0
	replace score=1 if s6bq01==1 | s6bq01==2
	replace score=2 if s6bq01==3 | s6bq01==4
	replace score=3 if s6bq01==5 | s6bq01==6 | s6bq01==7

	replace score=3-score if s6bq00==5 | s6bq00==8
	
		** check 
		tab s6bq01 score if s6bq00!=5 & s6bq00!=8
		tab s6bq01 score if s6bq00==5 | s6bq00==8
		
		bys hhid: egen cesd_count=count(score)
		bys hhid: egen cesd_total=total(score)
		
		replace cesd_total=. if cesd_count<8 // don't score if more than 2 are missing
		
		gen depressed=cesd_total>=10
		
		keep hhid depressed cesd_total
		duplicates drop
		
		ren depressed w3h_depressed
		ren cesd_total w3h_cesd_total
		
		gen indiv=1
		tempfile attitudes
		save `attitudes', replace

** Roster Information
	use "$dir/rawdata/LSMS Nigeria/2015/sect1_harvestw3.dta", clear // ROSTER

	count
	local samplesize=`r(N)'
	merge 1:1 hhid indiv using "$dir/rawdata/LSMS Nigeria/2015/sect2_harvestw3.dta", gen(merge_sec2) assert(master matched) // EDUCATION
	merge 1:1 hhid indiv using "$dir/rawdata/LSMS Nigeria/2015/sect3_harvestw3.dta", gen(merge_sec3) assert(master matched) // LABOR
	merge 1:1 hhid indiv using "$dir/rawdata/LSMS Nigeria/2015/sect4a_harvestw3.dta", gen(merge_sec4a) assert(master matched) // HEALTH
	merge 1:1 hhid indiv using `attitudes', gen(merge_sec6b) keep(master matched) // 2 from using that are unmatched

** Still in HH
gen w3h_inhh=s1q4a!=2

** Household size
gen temp=1 if w3h_inhh==1
bys hhid: egen w3h_hh_size=total(temp)
drop temp

** Number of children 12 or younger
gen temp=1 if s1q4<=12 & w3h_inhh==1
bys hhid: egen w3h_hh_under12=total(temp)
drop temp

** Number of youth 18 or younger
gen temp=1 if s1q4<=18 & w3h_inhh==1
bys hhid: egen w3h_hh_under18=total(temp)
drop temp

** Number of adults 60 or older
gen temp=1 if s1q4>=60 & !missing(s1q4) & s1q4!=999 & w3h_inhh==1
bys hhid: egen w3h_hh_over60=total(temp)
drop temp

** Adults 12 to 60
gen temp=1 if s1q4>12 & s1q4<60 & !missing(s1q4) & s1q4!=999 & w3h_inhh==1
bys hhid: egen w3h_hh_12to60=total(temp)
drop temp

** Marital status
gen w3h_married=(s1q7==1 | s1q7==2) if !missing(s1q7)
gen w3h_married_monog=(s1q7==1) if !missing(s1q7)
gen w3h_married_polyg=(s1q7==2) if !missing(s1q7)

** Female
gen w3h_female=(s1q2==2) if !missing(s1q2)

** Age
gen w3h_age=s1q4
replace w3h_age=. if w3h_age==999

** Age of spouse
sum indiv 
forvalues x=1/`r(max)' {
	gen age_person`x'_temp=s1q4 if indiv==`x' & !missing(s1q4)
	bys hhid: egen age_person`x'=max(age_person`x'_temp)
	drop age_person`x'_temp
}

** Average age of others in the household
sum indiv 
forvalues x=1/`r(max)' {
	replace age_person`x'=. if indiv==`x'
}
egen w3h_hh_avgage=rowmean(age_person*)

** Spouse age
sum indiv
forvalues x=1/`r(max)' {
	replace age_person`x'=. if s1q12!=`x'
}
egen w3h_spouse_age=rowmean(age_person*)

** Age gap
gen w3h_spouse_agegap=w3h_age-w3h_spouse_age

** Birthday
gen w3h_birthday=mdy(s1q6_month,s1q6_day,s1q6_year)
format w3h_birthday %td

** Birth year
gen w3h_birthyear=s1q6_year
recode w3h_birthyear 9999=. 9998=.
replace w3h_birthyear=2016-w3h_age if missing(w3h_birthyear)

** Highest education of individual
gen w3h_educ=s2aq9
replace w3h_educ=0 if s2aq6==2
label values w3h_educ s2aq9
replace w3h_educ=. if s2aq9==98

** Still in school
gen w3h_inschool=(s2aq13==1) if !missing(s2aq13)

** Literate
gen w3h_literate=(s2aq5==1) if !missing(s2aq5)

** Worked for a non-HH member
gen w3h_work_outsideHH=s3q4==1 if !missing(s3q4)
gen w3h_work_outsideHH_hrs=s3q4b
replace w3h_work_outsideHH_hrs=0 if w3h_work_outsideHH==0

** Worked in agriculture for a HH member
gen w3h_work_agHH=s3q5==1 if !missing(s3q5)
gen w3h_work_agHH_hrs=s3q5b
replace w3h_work_agHH_hrs=0 if w3h_work_agHH==0

** Worked in HH business
gen w3h_work_bizHH=s3q6==1 if !missing(s3q6)
gen w3h_work_bizHH_hrs=s3q6b
replace w3h_work_bizHH_hrs=0 if w3h_work_bizHH==0

** Any work in last 7 days (agriculture, for non-HH member, own account/business)
gen w3h_worked_last7days=0
replace w3h_worked_last7days=1 if s3q4==1
replace w3h_worked_last7days=1 if s3q5==1 
replace w3h_worked_last7days=1 if s3q6==1
replace w3h_worked_last7days=. if missing(s3q4) & missing(s3q5) & missing(s3q6)

** Hours worked in last 7 days
egen w3h_work_hrs=rowtotal(w3h_work_outsideHH_hrs w3h_work_agHH_hrs w3h_work_bizHH_hrs)
replace w3h_work_hrs=. if missing(w3h_work_outsideHH_hrs) & missing(w3h_work_agHH_hrs) & missing(w3h_work_bizHH_hrs)

** Works in agriculture
gen w3h_work_agri=(w3h_work_agHH==1 | s3q14==1 | s3q27==1) if (!missing(w3h_work_agHH) | !missing(s3q14) | !missing(s3q27))

** Industry of occupation
gen w3h_occ=s3q14
label values w3h_occ s3q14

** Highest education of biological father
gen w3h_father_educ=s1q21
label values w3h_father_educ s1q21
replace w3h_father_educ=. if w3h_father_educ==98

** Industry of occupation of biological father
gen w3h_father_occ=s1q22
label values w3h_father_occ s1q22

** Highest education of biological mother
gen w3h_mother_educ=s1q26
label values w3h_mother_educ s1q26
replace w3h_mother_educ=. if w3h_mother_educ==98

** Industry of occupation of biological mother
gen w3h_mother_occ=s1q27
label values w3h_mother_occ s1q27

** ID of father and mother (for linking)
gen w3h_father_id=s1q19 
gen w3h_mother_id=s1q24

** GEOGRAPHIC INFORMATION
gen w3h_geo_urban=(sector==1) if !missing(sector)
gen w3h_geo_state=state
gen w3h_geo_lga=lga
replace w3h_geo_lga=s1q31b if !missing(s1q31b) // LGA code for where the HH member moved
label var w3h_geo_urban "Urban residence"
label var w3h_geo_state "State of residence"
label var w3h_geo_lga "LGA of residence" 


** DISABILITIES

	** Difficulty seeing
		tab1 s4aq23, nol
		tab1 s4aq23
		gen w3h_diff_seei=1 if inlist(s4aq23, 2, 3, 4) 
		replace w3h_diff_seei=0 if inlist(s4aq23, 1)
		replace w3h_diff_seei=. if inlist(s4aq23, .)
		tab1 w3h_diff_seei

	** Difficulty hearing

		*n/a

	** Difficulty walking or climbing stairs	
		tab1 s4aq22b s4aq22c s4aq22d 
		gen diff_walk_up=(s4aq22b==2) if !missing(s4aq22b)
		gen diff_bend_over=(s4aq22c==2) if !missing(s4aq22c)
		gen diff_walk_more_me=(s4aq22d==2) if !missing(s4aq22d)
		gen w3h_diff_walk_clim=(														         ///
			diff_walk_up==1 | diff_bend_over==1 | diff_walk_more_me==1	                         ///
			)
		replace w3h_diff_walk_clim=. if (														 ///
			missing(diff_walk_up) &																 ///
			missing(diff_bend_over) & 														     ///
			missing(diff_walk_more_me)															 ///
			)
																								
		tab1 w3h_diff_walk_clim

	** Difficulty remembering or concentrating

		*n/a

	** Difficulty with self-care

		*n/a

	** Difficulty communicating
		*n/a

		
********************************************************************
********************************************************************

** HEALTH

	** Any illness or injury during the last 4 weeks
		gen w3h_ill_inj=(s4aq3==1) if !missing(s4aq3)

	** Had to stop usual activity in the past 4 weeks
		gen w3h_ill_inj_stopactivity=(s4aq4==1) if !missing(s4aq4)
		replace w3h_ill_inj_stopactivity=0 if w3h_ill_inj==0
		

** HH Size 

	merge m:1 hhid using "$dir/rawdata/LSMS Nigeria/2015/cons_agg_wave3_visit2.dta", assert(master matched) nogen

	gen w3h_urban_fromcons=rururb==1 if !missing(rururb)
	order w3h_urban, after(rururb)
	label var w3h_urban "Urban residence"

	ren totcons w3h_conspc

	ren hhsize w3h_hhsize_fromcons

keep hhid indiv w3*
order hhid indiv w3h_geo*
save "$dir/data/NGA_w3h_individual.dta", replace
